package com.bestcem.xm.user.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

/**
 * 省份区域枚举
 *
 * @author qiongyu.cui <qiongyu.cui@idiaoyan.com>
 * @version v1.0
 * @date 2022/5/16 18:05
 */
@Getter
@AllArgsConstructor
@NoArgsConstructor
public enum IpDistrictEnum {

    /**
     * 省份区域枚举
     */
    NORTH("North", Arrays.asList("黑龙江", "吉林", "辽宁", "北京", "北京市", "天津", "河北", "山西", "内蒙古", "陕西", "甘肃", "青海", "宁夏", "新疆", "西藏")),
    EAST("East", Arrays.asList("江苏", "浙江", "上海", "安徽", "山东", "湖北", "湖南", "江西")),
    SOUTH("South", Arrays.asList("广东", "广西", "福建", "云南", "贵州", "四川", "重庆", "海南", "深圳", "香港", "台湾", "澳门"));

    private String district;
    private List<String> provinces;

    public static String getDistrictByProvince(String province) {
        // 去除特殊关键字
        if (StringUtils.isNotEmpty(province)) {
            province = province.replace("市", "").replace("省", "");
        }
        return getDistrictByProvince(province, false);
    }

    /**
     * 通过省份获取对应的ip地区
     *
     * @param province      省
     * @param returnDefault 如果匹配不上，是否返回默认
     * @return java.lang.String
     * @author cuiqiongyu <qiongyu.cui@idiaoyan.cn>
     * @date 2022/05/16 18:23
     */
    public static String getDistrictByProvince(String province, boolean returnDefault) {
        String district = "";
        if (StringUtils.isNotBlank(province)) {
            district = Stream.of(values()).filter(v -> v.provinces.contains(province)).map(IpDistrictEnum::getDistrict).findFirst().orElse(null);
        }
        if (StringUtils.isBlank(district) && returnDefault) {
            return EAST.getDistrict();
        }
        return district;
    }

}
